package com.pai.gui.component;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JProgressBar;

import com.pai.entity.PaiTask;

/**
 * 已下载列表（mainFrame里的right栏）的cell布局。
 * @author Jeff
 *
 */
public class DownloadTaskCell extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private int index = 0;
	private DownloadTaskCell per2CellPanel=this;
	private DownloadTaskList baseList;
	private PaiTask  bean;
	
	//界面组件：
	private JLabel iconlabel;
	private JLabel filenamelabel;
	private JLabel fromlabel;
	private JLabel sizelabel;
	
	private JPopupMenu popmenu;
	//界面组件end。
	
	private int maxwidth = 200;
	private int maxhight = 80;
	
	public DownloadTaskCell(){
		super();
		init();
	}

	private void init() {
		this.setSize(new Dimension(maxwidth,maxhight));
		this.setOpaque(false);
		this.setLayout(null);
		
	}
	

	public JComponent getListCell(DownloadTaskList list, Object value) {
		
		this.bean = (PaiTask)value;
		this.baseList=list;
		
		//component定义
		iconlabel = new JLabel(new ImageIcon("res/icon/file.png"));
		iconlabel.setOpaque(false);
		filenamelabel = new JLabel(bean.getFileName());
		filenamelabel.setFont(new java.awt.Font("Arial", 1,12));
		filenamelabel.setForeground(Color.white);
		fromlabel = new JLabel(/*bean.getState() + " : " +*/bean.getFromUser().getUsername());
		fromlabel.setFont(new java.awt.Font("Arial", 1,12));
		fromlabel.setForeground(Color.white);
		sizelabel = new JLabel("Size:"+bean.getFileSize());
		sizelabel.setFont(new java.awt.Font("Arial", 1,12));
		sizelabel.setForeground(Color.white);
		popmenu = new JPopupMenu();
		JMenuItem openFileItem = new JMenuItem("open file");
		JMenuItem opendirItem = new JMenuItem("open dir");
		JMenuItem deleteItem = new JMenuItem("delete record");
		
		popmenu.add(openFileItem);
		popmenu.addSeparator();
		popmenu.add(opendirItem);
		popmenu.addSeparator();
		popmenu.add(deleteItem);
		
		
		//定位：
		iconlabel.setBounds(15, 15, 50, 50);
		filenamelabel.setBounds(65, 10, 140, 20);
		fromlabel.setBounds(65, 30, 140, 20);
		sizelabel.setBounds(65, 50, 140, 20);
		
		
		this.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseReleased(MouseEvent e) {
				if(e.isPopupTrigger()){
					//弹出右键菜单。
					popmenu.show(e.getComponent(), e.getX(), e.getY());
				}
			}			
		});
		
		openFileItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("open file = "+bean.getFileSrc());
				try {
					//打开文件的方法。
					Runtime.getRuntime().exec("explorer.exe "+ bean.getFileSrc());
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
			}
		});
		
		opendirItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("opendir = " + bean.getFileSrc());
				try {
					//打开文件夹的方法
					Runtime.getRuntime().exec("explorer.exe "+ (new File(bean.getFileSrc())).getParent());
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		
		deleteItem.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("delete record = " + bean.getFileName());
				//删除记录。
				baseList.getDownloadsource().removeCell(bean);
				
			}
		});
		
		
		this.add(iconlabel);
		this.add(filenamelabel);
		this.add(fromlabel);
		this.add(sizelabel);
		this.setMaximumSize(new Dimension(800, 80));
		return this;
	}
	
	/**
	 * 可以设置选中状态。后来方法弃用。空方法
	 * @param iss 
	 */
	public void setSelect(boolean iss) {
		
	}
}
